package pl.edu.mimuw.irs.rcp.controller.editor.roles.command;

import java.util.LinkedHashSet;
import java.util.Set;

import org.eclipse.gef.commands.Command;

import pl.edu.mimuw.irs.core.model.design.net.IRSRequirement;
import pl.edu.mimuw.irs.core.model.design.net.IRSWorkflow;
import pl.edu.mimuw.irs.core.model.design.resource.Requirement;
import pl.edu.mimuw.irs.core.model.design.resource.Role;

public abstract class AbstractWorkflowRoleCommand extends Command {

	// *** Attributes

	private IRSWorkflow workflow;
	private Role role;
	public Set<Requirement> affectedRequirements;
	public IRSWorkflow getWorkflow() {
		return workflow;
	}
	public Role getRole() {
		return role;
	}
	public Set<Requirement> getAffectedRequirements() {
		return affectedRequirements;
	}
	
	// *** Constructors
	
	public AbstractWorkflowRoleCommand(IRSWorkflow workflow, Role role) {
		this.workflow = workflow;
		this.role = role;
		this.affectedRequirements = new LinkedHashSet<Requirement>();
		
		Set<IRSRequirement> requirements = getWorkflow().getRequirements();
		for (IRSRequirement irsRequirement : requirements) {
			for (Requirement requirement : irsRequirement.getRequirements()) {
				if (requirement.getRolesMap().containsKey(getRole())) {
					affectedRequirements.add(requirement);
				}
			}
		}
	}
	
	// *** Methods

	/**
	 * Processes only requirements containing this role.
	 */
	protected abstract void execute(Requirement requirement);

	/**
	 * Processes only requirements containing this role.
	 */
	protected abstract void undo(Requirement requirement);
	
	@Override
	public void execute() {
		for (Requirement requirement : getAffectedRequirements()) {
			execute(requirement);
		}
	}

	@Override
	public void undo() {
		for (Requirement requirement : getAffectedRequirements()) {
			undo(requirement);
		}
	}
}
